import { defineStore } from "pinia"
import { ref } from "vue"

//用户模块
export const useUserStore = defineStore(
  "user", 
  () => {
    const token = ref<string>('')         //登录校验的token
    const IsRemember = ref<boolean>(false)  //是否记住我
    const username = ref<string>('')
    const setToken = (newToken: string) => {
      token.value = newToken
    }
    const setRememberMe = (remember: boolean) => {
      IsRemember.value = remember
    }
    const logout = function() {
      // 清除响应式数据
      token.value = ''
      username.value = ''
      IsRemember.value = false

      // 清除 persist 插件的存储
      localStorage.removeItem('user-store')
      // 清除本地存储
      localStorage.removeItem('refresh_token')
      localStorage.removeItem('username')
      localStorage.removeItem('user-store')  // persist插件存储
      
      sessionStorage.removeItem('access_token')
      sessionStorage.removeItem('refresh_token')
      
      // 清除cookie
      document.cookie = 'token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'
    }
    return { token, setToken, IsRemember, logout, username, setRememberMe }

  },
  {
    persist: true
  }
)